package self.vector.ding.dao.mybatis.generator.plugin;

import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

public class JsonFormatAnnotationForGetterMethodPlugin extends PluginAdapter {
	
	private final String JsonFormatAnnotation = "@JsonFormat(pattern=\"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")";

	public boolean validate(List<String> warnings) {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.mybatis.generator.api.PluginAdapter#modelGetterMethodGenerated(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.dom.java.TopLevelClass, org.mybatis.generator.api.IntrospectedColumn, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.Plugin.ModelClassType)
	 */
	@Override
	public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass,
			IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		if (isDateTypeProperty(introspectedColumn)) {
			topLevelClass.addImportedType("java.util.Date");
			topLevelClass.addImportedType("com.fasterxml.jackson.annotation.JsonFormat");
			method.addAnnotation(JsonFormatAnnotation);
			return true;
		}
		return true;
	}
	
	private boolean isDateTypeProperty(IntrospectedColumn introspectedColumn) {
		FullyQualifiedJavaType dateTypeInstance = FullyQualifiedJavaType.getDateInstance();
		if (introspectedColumn.getFullyQualifiedJavaType().equals(dateTypeInstance)) {
			return true;
		} else {
			return false;
		}
	}

}